<?php

namespace app\api\logic;

class Exam
{
    /**
     * 查询列表
     * @param array $param
     * @return array
     * @throws
     */
    public function getList($param = []): array
    {
        // 查询条件
        $where = [['status', '=', 1]];

        // 数据模型
        $examModel = new \app\common\model\Exam();

        // 查询字段
        $field = 'id,name';

        // 排序方式
        $order = ['sort' => 'asc'];

        // 查询数据
        $cache = false;
        $list = $examModel->field($field)->where($where)->order($order)->cache($cache)->select();

        // 显示头像
        $count = count($list);
        if (false)
        {
            $limit = 4;
            $avatarList = (new \app\common\model\User())->getUserAvatarForDisplay($count * $limit);
            if ($avatarList)
            {
                foreach ($list as $key => $val)
                {
                    $pop_max = $limit;
                    $avatars = [];
                    while ($pop_max--)
                    {
                        $avatars[] = array_pop($avatarList);
                    }
                    $list[$key]['avatars'] = $avatars;
                }
            }
        }

        // 填充人数
        foreach ($list as $key => $val)
        {
            $list[$key]['count'] = rand(10000, 20000);
        }

        // 返回
        return $list ? $list->toArray() : [];
    }
}